package com.example.hyw.szysteam.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.hyw.szysteam.POJO.Review;
import com.example.hyw.szysteam.R;

import java.util.ArrayList;

/**
 * Created by HYW on 2018/7/9.
 */

public class ReviewAdapter extends ArrayAdapter<Review>{
    private int resource;
    private ArrayList<Review> reviews;

    public ReviewAdapter(@NonNull Context context, int resource, @NonNull ArrayList<Review> objects) {
        super(context, resource, objects);
        this.resource = resource;
        reviews=objects;
    }

    @Nullable
    @Override
    public Review getItem(int position) {
        return reviews.get(position);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        Review review=getItem(position);
        View view;
        ViewHolder viewHolder;
        if(convertView==null){
            view= LayoutInflater.from(getContext()).inflate(resource,null);
            viewHolder =new ViewHolder();
            viewHolder.question=view.findViewById(R.id.question);
            viewHolder.answer=view.findViewById(R.id.answer);
            viewHolder.myAnswer=view.findViewById(R.id.myanswer);
            viewHolder.correct=view.findViewById(R.id.correct);
            view.setTag(viewHolder);
        }else {
            view=convertView;
            viewHolder=(ViewHolder)view.getTag();
        }
        viewHolder.question.setText(review.getQuestion());
        viewHolder.answer.setText(review.getAnswer());
        viewHolder.myAnswer.setText(review.getMyAnswer());
        if (!review.getAnswer().equals(review.getMyAnswer())){
            viewHolder.correct.setImageResource(R.drawable.ic_false);
        }else {
            viewHolder.correct.setImageResource(R.drawable.ic_true);
        }
        return view;
    }

    class ViewHolder{
        TextView question;
        TextView answer;
        TextView myAnswer;
        ImageView correct;
    }
}
